﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using com.lover.astd.common.model;
using com.lover.astd.common.config;
using System.Collections;
using System.Xml;
using com.lover.astd.common.manager;
using com.lover.astd.common;
using com.lover.astd.game.ui.server.impl.logger;
using com.lover.astd.common.logic;

namespace com.lover.astd.game.ui.server
{
    /// <summary>
    /// 服务接口
    /// </summary>
    public abstract class LogicServer : ISettings
    {
        protected ProtocolMgr _proto;
        protected MainForm _mainForm;
        public string ServerName = "";
        public string ServerReadableName = "";
        public virtual void release() 
        {
            _mainForm = null;
        }
        protected GameConfig getConfig() { return _mainForm.getConfig(); }
        protected Dictionary<string, string> getConfig(string confName) { return _mainForm.getConfig().getConfig(confName); }
        protected User getUser() { return _mainForm.GameUser; }
        /// <summary>
        /// 获取宝石价格, 1宝石=多少金币, 默认为0.2
        /// </summary>
        /// <returns></returns>
        protected double getGemPrice()
        {
            Dictionary<string, string> gconfs = getConfig("global");
            double price = 20;
            if (gconfs.ContainsKey("gem_price")) Double.TryParse(gconfs["gem_price"], out price);
            if (price > 0) price = 10 / price;
            else price = 0.5;
            return price;
        }
        protected List<int> generateIds(string id_string)
        {
            List<int> idstrs_to = new List<int>();
            if (id_string == null || id_string == "") return idstrs_to;
            string[] idstrs = id_string.Split(',');
            foreach (string strid in idstrs)
            {
                if (strid == null || strid == "") continue;
                int qhid = 0;
                Int32.TryParse(strid, out qhid);
                if (qhid == 0) continue;
                idstrs_to.Add(qhid);
            }
            return idstrs_to;
        }
        protected string getDataGridSelectedIds<T>(DataGridView dg)
        {
            if (dg == null) return "";
            BindingSource src = dg.DataSource as BindingSource;
            if (src == null) return "";
            List<T> objects = src.DataSource as List<T>;
            if (objects == null) return "";
            string selectedObjIds = "";
            int index = 0;
            for (int i=0, len=dg.Rows.Count; i<len; i++)
            {
                DataGridViewRow row = dg.Rows[i];
                DataGridViewCell cell = row.Cells[0];
                if ((bool)(cell.Value))
                {
                    int objId = (objects[index] as AsObject).Id;
                    selectedObjIds += objId + ",";
                }
                index++;
            }
            if (selectedObjIds.Length > 0) selectedObjIds = selectedObjIds.Substring(0, selectedObjIds.Length - 1);

            return selectedObjIds;
        }
        protected void renderDataGridToSelected<T>(DataGridView dg, string idsToSelected)
        {
            if (dg == null) return;
            BindingSource src = dg.DataSource as BindingSource;
            if (src == null) return;
            List<T> objects_all = src.DataSource as List<T>;
            if (objects_all == null) return;
            string[] objids_to_selected = idsToSelected.Split(',');
            int i = 0, j = 0, now_index = 0;
            // 首先, 先把所有的ischecked取消
            for (j = 0; j < objects_all.Count; j++) (objects_all[j] as AsObject).IsChecked = false;
            for (i = 0; i < objids_to_selected.Length; i++)
            {
                string heroidstr = objids_to_selected[i];
                int heroid = 0;
                if (Int32.TryParse(heroidstr, out heroid))
                {
                    for (j = 0; j < objects_all.Count; j++)
                    {
                        T obj = objects_all[j];
                        AsObject asobj = obj as AsObject;
                        if (asobj == null) continue;
                        if (asobj.Id == heroid)
                        {
                            asobj.IsChecked = true;
                            objects_all.RemoveAt(j);
                            objects_all.Insert(now_index, obj);
                            now_index++;
                            break;
                        }
                    }
                }
            }
        }

        public void setName(string name)
        {
            ServerName = name;
        }

        public string getName()
        {
            return ServerName;
        }


        public void setVariables(User u, GameConfig conf)
        {
            throw new NotImplementedException();
        }

        public void init()
        {
            throw new NotImplementedException();
        }

        #region ISettings 成员


        public virtual void saveSettings() { }
        public virtual void renderSettings() { }
        public virtual void loadDefaultSettings() { }

        #endregion
    }
}
